using FastDevTemplate.Api.Attribute;
using FastDevTemplate.Api.Base;
using FastDevTemplate.Api.Model;
using FastDevTemplate.Api.Model.Base;
using FastDevTemplate.Service.Interface;
using FastDevTemplate.Service.Interface.DTO;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace FastDevTemplate.Api.Controllers;

[EnableCors("AllowSpecificOrigin")]
public class RoleController : BaseController
{
    private readonly IRoleService _roleService;
    private readonly IUserService _userService;

    public RoleController(IRoleService roleService, IUserService userService)
    {
        _roleService = roleService;
        _userService = userService;
    }

    [HttpPost("add")]
    [PermissionVerification(id:"uc/role/add", describe:"添加角色", client:"uc")]
    public async Task<HttpResult<KeyModel<string>>> Add([FromBody] RoleAddDto model)
    {
        if (!ValidateModel(model).isValid)
            return BadResult<KeyModel<string>>();
        var result = await _roleService.AddAsync(model);
        if (result == null)
            return BadResult<KeyModel<string>>();
        return OkResult(new KeyModel<string>(result.ID));
    }

    [HttpPost("modifyInfo")]
    [PermissionVerification(id:"uc/role/modify", describe:"修改角色信息", client:"uc")]
    public async Task<HttpResult> ModifyInfo([FromBody] ModifyRequestModel<string, RoleModifyDto> model)
    {
        if (!ValidateModel(model.Data).isValid)
            return BadResult();
        var result = await _roleService.ModifyAsync(model.ID, model.Data);
        if (result == null)
            return BadResult();
        return OkResult();
    }

    [HttpPost("remove")]
    [PermissionVerification(id:"uc/role/remove", describe:"删除角色", client:"uc")]
    public async Task<HttpResult> Remove([FromBody] KeyModel<string> model)
    {
        var result = await _roleService.RemoveAsync(model.ID);
        if (result == null)
            return BadResult();
        return OkResult();
    }

    [HttpPost("getByID")]
    [PermissionVerification(id:"uc/role/get", describe:"获取角色", client:"uc")]
    public async Task<HttpResult<RoleGetResultDto>> GetByID([FromBody] KeyModel<string> model)
    {
        var result = await _roleService.GetByIdAsync(model.ID);
        return CheckResult(result);
    }

    [HttpPost("getList")]
    [PermissionVerification(id:"uc/role/get", describe:"获取角色", client:"uc")]
    public async Task<HttpResult<IEnumerable<RoleResultDto>>> GetList()
    {
        var result = await _roleService.GetSetAsync();
        return OkResult(result);
    }

    [HttpPost("getListByUser")]
    [PermissionVerification(id:"uc/role/get", describe:"获取角色", client:"uc")]
    public async Task<HttpResult<IEnumerable<RoleResultDto>>> GetListByUser([FromBody] RoleQueryByUserModels model)
    {
        var result = await _roleService.GetSetAsync(
            item => item.UserRoles.Any(u => u.User.UserName == model.UserName));
        return OkResult(result);
    }

    [HttpPost("addPermissionList")]
    [PermissionVerification(id:"uc/role/set-permission", describe:"设置角色权限", client:"uc")]
    public async Task<HttpResult> AddPermissionList([FromBody] ModifyRequestModel<string, string[]> model)
    {
        var result = await _roleService.AddPermissionListAsync(model.ID, model.Data);
        if (result <= 0)
            return BadResult();
        return OkResult();
    }

    [HttpPost("removePermissionList")]
    [PermissionVerification(id:"uc/role/set-permission", describe:"设置角色权限", client:"uc")]
    public async Task<HttpResult> RemovePermissionList([FromBody] ModifyRequestModel<string, string[]> model)
    {
        var result = await _roleService.RemovePermissionListAsync(model.ID, model.Data);
        if (result <= 0)
            return BadResult();
        return OkResult();
    }
}